package com.example.last.controller;

import com.example.last.common.Result;
import com.example.last.entity.Admin;
import com.example.last.entity.Params;
import com.example.last.entity.User;
import com.example.last.service.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RequestMapping("/user")
@RestController
public class UserController {
    @Resource
    private UserService userService;
    
    @GetMapping("/search")
    public Result findBySearch(@Param("params") Params params){
        PageInfo<User> info = userService.findBySearch(params);
        return Result.success(info);
    }

    //新增信息
    @PostMapping
    public Result save(@RequestBody User user){
        if(user.getId() == null){
            userService.add(user);
        }else{
            userService.update(user);
        }
        return Result.success();
    }
    @GetMapping("/username/{username}")
    public Result findByName(@PathVariable String username){
        User user = userService.findByName(username);
        return Result.success(user);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        userService.delete(id);
        return Result.success();
    }


}
